<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
/* ======================================================================*\
  || #################################################################### ||
  || # ND.Tiep CODER Tutorial                                           # ||
  || # ---------------------------------------------------------------- # ||
  || # Copyright ©2013 Nguyen Dinh Tiep. All Rights Reserved.           # ||
  || # Please do not remove this comment lines.                         # ||
  || # -------------------- LAST MODIFY INFORMATION ------------------- # ||
  || # Author by: Nguyen Dinh Tiep                                      # ||
  || #################################################################### ||
  \*====================================================================== */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*\
  |*-------Please specify source when using my code or a part of them-----*|
  \*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

require __LIBRARY_PATH.'/Form/Val.php';
class Form
{
    
  
    private $_currentItem = null;
    

    private $_postData = array();
    
  
    private $_val = array();
    
 
    private $_error = array();
    

    public function __construct() 
    {
        $this->_val = new Val();
    }
    

    public function post($field)
    {
        if(strpos($_POST[$field], "<?")==true){
            $this->_error['disablephp'] = "Hệ thống phát hiện ký tự không cho phép";
        }
        if(strpos($_POST[$field], "?>")==true){
            $this->_error['disablephp'] = "Hệ thống phát hiện ký tự không cho phép";
        }
        if(strpos($_POST[$field], "<%")==true){
            $this->_error['disablephp'] = "Hệ thống phát hiện ký tự không cho phép";
        }
        if(strpos($_POST[$field], "%>")==true){
            $this->_error['disablephp'] = "Hệ thống phát hiện ký tự không cho phép";
        }
        $this->_postData[$field] = $_POST[$field];
        $this->_currentItem = $field;
        
        return $this;
    }
    

    public function fetch($fieldName = false)
    {
        if ($fieldName) 
        {
            if (isset($this->_postData[$fieldName]))
            return $this->_postData[$fieldName];
            
            else
            return false;
        } 
        else 
        {
            return $this->_postData;
        }
        
    }

    public function val($typeOfValidator, $arg = null)
    {
        if ($arg == null)
        $error = $this->_val->{$typeOfValidator}($this->_postData[$this->_currentItem]);
        else
        $error = $this->_val->{$typeOfValidator}($this->_postData[$this->_currentItem], $arg);
        
        if ($error)
        $this->_error[$this->_currentItem] = $error;
        
        return $this;
    }
    

    public function submit()
    {
        if (empty($this->_error)) 
        {
            return true;
        } 
        else 
        {
            $str = '';
            foreach ($this->_error as $key => $value)
            {
               "Có lỗi ở trường : " .$str .= $key . ' => ' . $value . "<br/>";
            }
    
            return $str;
        }
    }
}